package it.polimi.swim.session;

import java.util.ArrayList;
import java.util.List;

import it.polimi.swim.entity.Friend;
import it.polimi.swim.entity.SystemUser;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.jboss.ejb3.annotation.RemoteBinding;

@Stateless
@RemoteBinding(jndiBinding = FriendManagerBean.REMOTE)
public class FriendManagerBean implements FriendManagerBeanRemote {

	public static final String REMOTE = "FriendManagerBean/remote";
	@PersistenceContext(unitName = "SWIMv2")
	private EntityManager em;
	
	public FriendManagerBean() {

	}

	@Override
	public SystemUser addFriend(int idSender, int idReceiver) {

		return null;
	}

	@Override
	public SystemUser setFriendshipRequest(int idSender, int Receiver,
			String status) {

		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SystemUser> showMyFriendshipRequest(int idUser) {
		List<Friend> myFriendsList;
		Query q;		
		System.out.println("query");
		q = em.createNamedQuery("Friend.getFriendshipReceiver");
		q.setParameter("user", idUser);
		List<SystemUser> friends = q.getResultList();
		return friends;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SystemUser> showFriends(int idUser) {
		
		List<SystemUser> myFriendsList;
		Query q;		
		System.out.println("query");
		q = em.createNamedQuery("Friend.getUserFriend");
		q.setParameter("user", idUser);
		myFriendsList = q.getResultList();

		return myFriendsList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void createFriendRequest(SystemUser sender, SystemUser receiver) {
		Query q, q1;		
		q = em.createNamedQuery("Friend.getFriendBy2Users");
		q.setParameter("idSender", sender.getId());
		q.setParameter("idReceiver", receiver.getId());
		q1 = em.createNamedQuery("Friend.getFriendBy2Users");
		q1.setParameter("idSender", receiver.getId());
		q1.setParameter("idReceiver", sender.getId());
		
		if(q.getResultList().size()==0 && q1.getResultList().size()==0){
			q = em.createNamedQuery("SystemUser.getUserById");
			q.setParameter("id", sender.getId());
			sender = (SystemUser) q.getSingleResult();
			q = em.createNamedQuery("SystemUser.getUserById");
			q.setParameter("id", receiver.getId());
			receiver = (SystemUser) q.getSingleResult();
	
			Friend friend = new Friend();
			friend.setIdReceiver(receiver.getId());
			friend.setIdSender(sender.getId());
			friend.setStatus(false);
			em.persist(friend);
			
			sender.getFriendRequest().add(friend);
			receiver.getFriendRequest().add(friend);
			em.persist(sender);
			em.persist(receiver);
		}
		
	}

	@Override
	public List<SystemUser> viewFriendshipRequest(int idUser) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<SystemUser> showOtherFriendshipRequest(int id) {
		List<Friend> myFriendsList;
		Query q;		
		System.out.println("query");
		q = em.createNamedQuery("Friend.getFriendshipSender");
		q.setParameter("user", id);
		List<SystemUser> friends = q.getResultList();
		return friends;
	}

	@Override
	public void acceptFriendship(int idReceiver, int idSender) {
		Friend friend;
		Query q;		
		System.out.println("query");
		q = em.createNamedQuery("Friend.getFriendById");
		q.setParameter("idSender", idSender);
		q.setParameter("idReceiver", idReceiver);
		friend = (Friend) q.getSingleResult();
		friend.setStatus(true);
		em.persist(friend);
	}

}
